package com.sat.order.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sat.goods.config.SkuFeign;
import com.sat.order.config.OrderRabbitMqConfig;
import com.sat.order.mapper.OrderItemMapper;
import com.sat.order.mapper.OrderMapper;
import com.sat.order.model.Order;
import com.sat.order.model.OrderItem;
import com.sat.order.service.OrderService;
import net.bytebuddy.asm.Advice;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void add(Long[] skuIds, String receiver_contact, String receiver_mobile, String receiver_address) {

        //生成订单，将订单需要的值都设置进去
        Order order = new Order();
        String  orderId = IdWorker.getId()+"";
        order.setId(orderId);
        Date date = new Date();
        order.setCreateTime(date);
        order.setUpdateTime(date);
        String username="admin";//设置用户名称、一般都是登录用户中获取
        order.setUsername(username);
        order.setReceiverMobile(receiver_mobile);//设置收货人电话
        order.setReceiverAddress(receiver_address);//设置收获地址
        order.setReceiverContact(receiver_contact);//设置收货人
        order.setSourceType("1");
        order.setOrderStatus("0");//设置订单状态
        order.setPostFee(0);//设置免邮
        Integer totalNum=0;
        Integer totalMoney=0;

        //从redis中获取购物车信息，设置到order中
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("cart_" + username);
        for (Long skuId : skuIds) {
            OrderItem orderItem = (OrderItem) boundHashOperations.get(skuId);
            //之前我们获取的商品详情是没有id的，我们要设置id，然后将商品详情保存到数据库中
            String  id1 = IdWorker.getId()+"";//设置订单详情的id
            orderItem.setId(id1); //设置订单详情表中的订单id
            orderItem.setOrderId(orderId);
            //把数据详情表保存到数据库
            orderItemMapper.insert(orderItem);
            //同时删除redis中的数据
            boundHashOperations.delete(skuId);//根据skuId删除redis中订单详情信息
            totalNum+=orderItem.getNum();
            totalMoney+=orderItem.getMoney();

            //调用goods模块中的下单后更新库存的接口来更新商品的数量
            skuFeign.updateInventory(skuId.toString(),orderItem.getNum());

        }
        order.setTotalNum(totalNum);//设置订单中商品总数量
        order.setTotalMoney(totalMoney);//设置订单的总价
        order.setPreMoney(0);//表示没有优惠券
        order.setPayType("1");//设置支付类型
        //将订单插入订单表
        orderMapper.insert(order);

        //生成订单后，就向队列中发送一个消息，并且设置延迟队列，要是用户在规定时间
        //没有支付的话，就回滚库存，传入的消息是订单id，通过订单id查询用户支付状态
        //支付了就不处理，没支付就回滚库存

        //将orderId发送到消息队列
        rabbitTemplate.convertAndSend(OrderRabbitMqConfig.TOPIC_EXCHANGE,"order.sat.lingyi",orderId);

    }
}
